package ut.util;
import ut.data.User;
import ut.data.Profile;
import ut.data.Record;
import java.util.HashMap;
public class Cache{
	// 已下用來快取 User Message Count
	private static HashMap<Integer, Integer> messageCounts = new HashMap<Integer, Integer>(256);
	public static Integer getMessageCount(int id){
		synchronized(messageCounts){
			return messageCounts.get(id);
		}
	}
	public static void cacheMessageCount(int id, Integer messageCount){
		synchronized(messageCounts){
			if(messageCounts.size() > 250){
				messageCounts.clear();
			}
			messageCounts.put(id, messageCount);
		}
	}
	public static void removeMessageCount(int id){
		synchronized(messageCounts){
			messageCounts.remove(id);
		}
	}
	// 以下用來快取 User Name
	private static HashMap<Integer, String> userNames = new HashMap<Integer, String>(256);
	public static String getUserName(int id){
		synchronized(userNames){
			return userNames.get(id);
		}
	}
	public static void cacheUserName(int id, String userName){
		synchronized(userNames){
			if(userNames.size() > 250){
				userNames.clear();
			}
			userNames.put(id, userName);
		}
	}
	// 以下用來快取 Profile
	private static HashMap<Integer, Profile> profiles = new HashMap<Integer, Profile>(64);
	public static Profile getProfile(int id){
		synchronized(profiles){
			return profiles.get(id);
		}
	}
	public static void cacheProfile(int id, Profile profile){
		synchronized(profiles){
			if(profiles.size() > 60){
				profiles.clear();
			}
			profiles.put(id, profile);
		}
	}
	public static void removeProfile(int id){
		synchronized(profiles){
			profiles.remove(id);
		}
	}
	// 以下用來快取 Record
	private static HashMap<Integer, Record> records = new HashMap<Integer, Record>(64);
	public static Record getRecord(int id){
		synchronized(records){
			return records.get(id);
		}
	}
	public static void cacheRecord(int id, Record record){
		synchronized(records){
			if(records.size() > 60){
				records.clear();
			}
			records.put(id, record);
		}
	}
	public static void removeRecord(int id){
		synchronized(records){
			records.remove(id);
		}
	}
}